﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics.CodeAnalysis;

namespace CoolEngine.Engine
{
    [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Tokeniser", Justification = "Learn to spell.")]
    public static class Tokeniser
    {        
        private static Regex regex = new Regex(@"\x22[^\x22]*\x22|\S+",RegexOptions.IgnoreCase | RegexOptions.Compiled);
        /// <summary>
        /// Tokenises the specified string.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns>An array of strings which have been split by whitespace, except for those in a "".</returns>
        [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Tokenise", Justification = "Learn to spell.")]
        public static string[] Tokenise(this string source)
        {            
            source = source.Trim();
            List<string> tokens = new List<string>();
            MatchCollection collection = regex.Matches(source);
            foreach (Match match in collection)
            {
                tokens.Add(match.Value.Replace("\"", ""));
            }
            return tokens.ToArray();
        }
    }
}
